//
//  UserPreferencesController.m
//  RunSketch
//
//  Created by Brian on 10/12/09.
//  Copyright 2009 Tellart. All rights reserved.
//

#import "UserPreferencesController.h"


@implementation UserPreferencesController

@synthesize
    enableRemoteIOPack,
    remoteIOPackHostName,
    useRemoteIOPackFlowControl,
    enableLocalIOPack,
    localIOPackDevPath,
    useLocalIOPackFlowControl,
    useSensorBoost,
    lastShutdownStatus
;


-(id)init
{
    if (self = [super init])
    {
        NSLog(@"[UserPreferencesController init]");
        [self revert];
    }
    return self;
}

- (void)dealloc {
    NSLog(@"[SketchUpdateController dealloc]");
    [remoteIOPackHostName release];
    [localIOPackDevPath release];
    [lastShutdownStatus release];
	[super dealloc];
}



- (void)revert {
	NSLog(@"UsePreferencesController: Loading user preferences from persistent storage...");
	//use the standard defaults
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    self.enableRemoteIOPack = [defaults boolForKey:@"enableRemoteIOPack"];
    self.remoteIOPackHostName = [defaults stringForKey:@"remoteIOPackHostName"];
    self.useRemoteIOPackFlowControl = [defaults boolForKey:@"useRemoteIOPackFlowControl"];
    
    self.enableLocalIOPack = [defaults boolForKey:@"enableLocalIOPack"];
    self.localIOPackDevPath = [defaults stringForKey:@"localIOPackDevPath"];
    if (self.localIOPackDevPath == nil) self.localIOPackDevPath = @"/dev/tty.iap";
    self.useLocalIOPackFlowControl = [defaults boolForKey:@"useLocalIOPackFlowControl"];
    
    self.useSensorBoost = [defaults boolForKey:@"useSensorBoost"];
    
    self.lastShutdownStatus = [defaults stringForKey:@"lastShutdownStatus"];
	
    [self log];
    
	//clean up
	//[defaultSketchServerName release];
}
- (void)store {
	NSLog(@"UsePreferencesController: Storing user preferences in persistent storage...");
	//use the standard defaults
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setBool:self.enableRemoteIOPack forKey:@"enableRemoteIOPack"];
    [defaults setObject:self.remoteIOPackHostName forKey:@"remoteIOPackHostName"];
    [defaults setBool:self.useRemoteIOPackFlowControl forKey:@"useRemoteIOPackFlowControl"];
    
    [defaults setBool:self.enableLocalIOPack forKey:@"enableLocalIOPack"];
    [defaults setObject:self.localIOPackDevPath forKey:@"localIOPackDevPath"];
    [defaults setBool:self.useLocalIOPackFlowControl forKey:@"useLocalIOPackFlowControl"];
    
    [defaults setBool:self.useSensorBoost forKey:@"useSensorBoost"];
    
    [defaults setObject:self.lastShutdownStatus forKey:@"lastShutdownStatus"];
	
    [self log];
}
- (void)log {
    NSLog(@"-- UserPreferencesController --");
    NSLog(@"   enableRemoteIOPack = %i", self.enableRemoteIOPack);
    NSLog(@"   remoteIOPackHostName = %@", self.remoteIOPackHostName);
    NSLog(@"   useRemoteIOPackFlowControl = %i", self.useRemoteIOPackFlowControl);
    NSLog(@"   enableLocalIOPack = %i", self.enableLocalIOPack);
    NSLog(@"   localIOPackDevPath = %@", self.localIOPackDevPath);
    NSLog(@"   useLocalIOPackFlowControl = %i", self.useLocalIOPackFlowControl);
    NSLog(@"   useSensorBoost = %i", self.useSensorBoost);
    NSLog(@"   lastShutdownStatus = %@", self.lastShutdownStatus);
    NSLog(@"-------------------------------");
}

@end
